﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKArchUtils.Utils;
using YJKRevitWallModule.Properties;

namespace YJKRevitWallModule.WallModule.EditWall
{
	public partial class ViewResultsForm : System.Windows.Forms.Form
	{
		public ViewResultsForm(UIDocument uiDoc, List<ElementId> elementsIdList)
		{
			this.m_UIDoc = uiDoc;
			this.m_ElementsIdList = elementsIdList;
			this.InitializeComponent();
			this.btnExtend.Image = Resources.Unfold;
			this.m_Unfold = true;
			this.m_ShowAll = true;
			this.m_FormOffset = this.labelText.Height / 3 * 5;
			base.Height -= this.m_FormOffset;
			if (this.m_ElementsIdList.Count == 0)
			{
				this.btnPrevious.Enabled = false;
				this.btnView.Enabled = false;
				this.btnNext.Enabled = false;
				this.trackBarZoom.Enabled = false;
				this.btnOk.Enabled = false;
			}
			this.m_ShowIndex = 0;
			foreach (UIView uiview in this.m_UIDoc.GetOpenUIViews())
			{
				if (uiview.ViewId.Equals(this.m_UIDoc.ActiveView.Id))
				{
					this.m_ActiveUIView = uiview;
					break;
				}
			}
		}

		public void SetOutput(string output)
		{
			if (this.m_ElementsIdList.Count == 0)
			{
				this.lbOutput.Text = "没有搜索到需要处理的外墙";
			}
			else
			{
				this.lbOutput.Text = output;
			}
			this.lbOutput.Left = (base.Width - this.lbOutput.Width) / 2;
		}

		private void btnView_Click(object sender, EventArgs e)
		{
			if (this.m_ElementsIdList.Count > 0)
			{
				this.HighlightWall(this.m_ElementsIdList);
				this.m_UIDoc.ShowElements(this.m_ElementsIdList);
				this.m_ShowAll = true;
			}
		}

		private void btnPrevious_Click(object sender, EventArgs e)
		{
			this.m_ShowIndex--;
			if (this.m_ShowIndex == -1)
			{
				this.m_ShowIndex = this.m_ElementsIdList.Count - 1;
			}
			Element element = this.m_UIDoc.Document.GetElement(this.m_ElementsIdList[this.m_ShowIndex]);
			new List<Element>();
			this.ShowElement(element);
			this.m_ShowAll = false;
		}

		private void btnNext_Click(object sender, EventArgs e)
		{
			this.m_ShowIndex++;
			if (this.m_ShowIndex == this.m_ElementsIdList.Count)
			{
				this.m_ShowIndex = 0;
			}
			Element element = this.m_UIDoc.Document.GetElement(this.m_ElementsIdList[this.m_ShowIndex]);
			new List<Element>();
			this.ShowElement(element);
			this.m_ShowAll = false;
		}

		private void ShowElement(Element ele)
		{
            // 计算一个对象的边界，并显示这个构件
			this.HighlightWall(ele);
			BoundingBoxXYZ boundingBoxXYZ = ele.get_BoundingBox(this.m_UIDoc.ActiveView);
			if (boundingBoxXYZ == null)
			{
				Wall wall = ele as Wall;
				if (wall == null)
				{
					return;
				}
				Curve curve = (wall.Location as LocationCurve).Curve;
				if (curve.GetType() == typeof(Line))
				{
					BoundingBoxUV boundingBoxUV = Geometry.CalculateBoundingBox2D(curve as Line);
					boundingBoxXYZ = new BoundingBoxXYZ();
					boundingBoxXYZ.Min = new XYZ(boundingBoxUV.Min.U, boundingBoxUV.Min.V, 0.0);
					boundingBoxXYZ.Max = new XYZ(boundingBoxUV.Max.U, boundingBoxUV.Max.V, 0.0);
				}
				else if (curve.GetType() == typeof(Arc))
				{
					BoundingBoxUV boundingBoxUV2 = Geometry.CalculateBoundingBox2D(curve as Arc);
					boundingBoxXYZ = new BoundingBoxXYZ();
					boundingBoxXYZ.Min = new XYZ(boundingBoxUV2.Min.U, boundingBoxUV2.Min.V, 0.0);
					boundingBoxXYZ.Max = new XYZ(boundingBoxUV2.Max.U, boundingBoxUV2.Max.V, 0.0);
				}
			}
			double num = 2.0;
			int value = this.trackBarZoom.Value;
			if (value > 0)
			{
				num *= (double)(value + 1);
			}
			else if (value < 0)
			{
				num /= (double)(-(double)value + 1);
			}
			XYZ xyz = (boundingBoxXYZ.Min - boundingBoxXYZ.Max).Normalize();
			XYZ xyz2 = -xyz;
			double length = (boundingBoxXYZ.Min - boundingBoxXYZ.Max).GetLength();
			XYZ xyz3 = boundingBoxXYZ.Min + xyz * length * num;
			XYZ xyz4 = boundingBoxXYZ.Max + xyz2 * length * num;
			this.m_ActiveUIView.ZoomAndCenterRectangle(xyz3, xyz4);
		}

		private void HighlightWall(Element ele)
		{
			RevitVersionFuncs.ClearSelection(this.m_UIDoc.Selection);
			RevitVersionFuncs.AddToSelection(this.m_UIDoc.Selection, ele);
		}

		private void HighlightWall(List<ElementId> idList)
		{
			List<Element> list = new List<Element>();
			foreach (ElementId elementId in idList)
			{
				Element element = this.m_UIDoc.Document.GetElement(elementId);
				list.Add(element);
			}
			RevitVersionFuncs.SetSelection(this.m_UIDoc.Selection, list);
		}

		private void UnHighlightWall()
		{
			RevitVersionFuncs.ClearSelection(this.m_UIDoc.Selection);
		}

		private void btnExtend_Click(object sender, EventArgs e)
		{
			if (this.m_Unfold)
			{
				this.btnExtend.Image = Resources.Fold;
				base.Height += this.m_FormOffset;
				this.m_Unfold = false;
				return;
			}
			this.btnExtend.Image = Resources.Unfold;
			base.Height -= this.m_FormOffset;
			this.m_Unfold = true;
		}

		private void trackBarZoom_Scroll(object sender, EventArgs e)
		{
			if (this.m_ShowIndex >= this.m_ElementsIdList.Count || this.m_ShowIndex == -1)
			{
				return;
			}
			if (!this.m_ShowAll)
			{
				Element element = this.m_UIDoc.Document.GetElement(this.m_ElementsIdList[this.m_ShowIndex]);
				if (element == null)
				{
					return;
				}
				this.ShowElement(element);
			}
		}

		private void ViewResultsForm_Load(object sender, EventArgs e)
		{
			this.LoadCompornentData();
		}

		private void ViewResultsForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveCompornentData();
		}

		private void LoadCompornentData()
		{
			string path = Path.GetTempPath() + "ViewResultsForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				this.trackBarZoom.Value = 0;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			this.trackBarZoom.Value = Convert.ToInt32(streamReader.ReadLine());
			try
			{
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
				base.Location = new System.Drawing.Point(x, y);
			}
			catch (Exception)
			{
				streamReader.Close();
				fileStream.Close();
				return;
			}
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveCompornentData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "ViewResultsForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.trackBarZoom.Value);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void btnPrevious_MouseMove(object sender, MouseEventArgs e)
		{
			if (this.toolTipShowOutWall.GetToolTip(this.btnPrevious) == this.btnPrevious.Tag.ToString())
			{
				return;
			}
			this.toolTipShowOutWall.SetToolTip(this.btnPrevious, this.btnPrevious.Tag.ToString());
		}

		private void btnView_MouseMove(object sender, MouseEventArgs e)
		{
			if (this.toolTipShowOutWall.GetToolTip(this.btnView) == this.btnView.Tag.ToString())
			{
				return;
			}
			this.toolTipShowOutWall.SetToolTip(this.btnView, this.btnView.Tag.ToString());
		}

		private void btnNext_MouseMove(object sender, MouseEventArgs e)
		{
			if (this.toolTipShowOutWall.GetToolTip(this.btnNext) == this.btnNext.Tag.ToString())
			{
				return;
			}
			this.toolTipShowOutWall.SetToolTip(this.btnNext, this.btnNext.Tag.ToString());
		}

		private void btnExtend_MouseMove(object sender, MouseEventArgs e)
		{
			if (this.toolTipShowOutWall.GetToolTip(this.btnExtend) == this.btnExtend.Tag.ToString())
			{
				return;
			}
			this.toolTipShowOutWall.SetToolTip(this.btnExtend, this.btnExtend.Tag.ToString());
		}

		private UIDocument m_UIDoc;

		private UIView m_ActiveUIView;

		private List<ElementId> m_ElementsIdList = new List<ElementId>();

		private bool m_Unfold = true;

		private int m_ShowIndex;

		private bool m_ShowAll = true;

		private int m_FormOffset;
	}
}
